package com.hzsd.zengbanyi.biaodan_text.events;

import java.util.ArrayList;


import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.hzsd.zengbanyi.biaodan_text.bean.WorkDAO;
import com.hzsd.zengbanyi.biaodan_text.utils.TextChange;
import com.hzsd.zengbanyi.biaodan_text.view.FixingAgentView;

public class TaskSceneEvent {
	private Context context;
	private WorkDAO workDAO;
	private String Fixing;
	
	public TaskSceneEvent(Context context){
		this.context = context;
		workDAO = new WorkDAO(context);
	}
	/**
	 * 编辑框 相类似控件保存数据方法  
	 * @param editText
	 * @param taskSceneId
	 * @param extend_name
	 * @param type
	 */
	public void editTextSaveData(EditText editText,String taskSceneId,String extend_name,String type){
		String str = workDAO.getTaskSceneMoreField(taskSceneId,extend_name,type);
		switch (extend_name) {
		case "小时/每日":
			if ("".equals(str)) {
				str = "24";
				workDAO.updateTaskSceneMoreField(taskSceneId, str,
						extend_name,type);
			}
			break;
		case "生产负荷":
			if ("".equals(str)) {
				// 数据库中负荷没有值，则设置为默认值：100%；并添加到数据库中
				str = "100%";
				workDAO.updateTaskSceneMoreField(taskSceneId, str,
						extend_name,type);
			}
			break;
		case "采样工具":
			if ("".equals(str)) {
				str = "采样桶";
				workDAO.updateTaskSceneMoreField(taskSceneId, str,
						extend_name,type);
			}
			break;
		case "采样位置及层次":
			if ("".equals(str)) {
				str = "表层";
				workDAO.updateTaskSceneMoreField(taskSceneId, str,
						extend_name,type);
			}
			break;
		case "规范化情况":
			if ("".equals(str)) {
				str = "规范";
				workDAO.updateTaskSceneMoreField(taskSceneId, str,
						extend_name,type);
			}
			break;
		case "气温":
			if ("".equals(str)) {
				str = "25";
				workDAO.updateTaskSceneMoreField(taskSceneId, str,
						extend_name,type);
			}
			break;
		case "样品可能含有的干扰物":
			if ("".equals(str)) {
				str = "无";
				workDAO.updateTaskSceneMoreField(taskSceneId, str,
						extend_name,type);
			}
			break;

		default:
			break;
		}
		
		editText.setText(str);
		editText.addTextChangedListener(new TextChange(context,taskSceneId,extend_name,type,editText));
	}
	
	/**
	 * 固定剂加入情况
	 */
	public void fixingAgent(final TextView textView,final String taskSceneId,final String extend_name,final String type) {
		Fixing = workDAO.getTaskSceneMoreField(taskSceneId,extend_name,type).replace("|", "、");
		textView.setText(Fixing.length() > 0 ? Fixing.substring(1)
				: Fixing);

		textView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ArrayList<String> arrayList;
				arrayList = FixingAgentView.getFixingAgentWRY();
				String fixingagent = workDAO.getTaskSceneMoreField(taskSceneId,extend_name,type);
				View view = FixingAgentView
						.initFixingAgentView(context, taskSceneId,extend_name,type,
								fixingagent, arrayList);
				AlertDialog.Builder builder = new AlertDialog.Builder(
						context);
				builder.setTitle(extend_name);
				builder.setView(view);
				builder.setPositiveButton("确定",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								Fixing = workDAO.getTaskSceneMoreField(taskSceneId,extend_name,type)
										.replace("|", "、");
								textView.setText(Fixing.length() > 0 ? Fixing
										.substring(1) : Fixing);
							}
						});
				builder.create().show();

			}
		});
	}
	

}
